{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "278b6c63",
   "metadata": {},
   "source": [
    "# llamafile\n",
    "\n",
    "Let's load the [llamafile](https://github.com/Mozilla-Ocho/llamafile) Embeddings class.\n",
    "\n",
    "## Setup\n",
    "\n",
    "First, the are 3 setup steps:\n",
    "\n",
    "1. Download a llamafile. In this notebook, we use `TinyLlama-1.1B-Chat-v1.0.Q5_K_M` but there are many others available on [HuggingFace](https://huggingface.co/models?other=llamafile).\n",
    "2. Make the llamafile executable.\n",
    "3. Start the llamafile in server mode.\n",
    "\n",
    "You can run the following bash script to do all this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43ef6dfa-9cc4-4552-8a53-5df523afae7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "# llamafile setup\n",
    "\n",
    "# Step 1: Download a llamafile. The download may take several minutes.\n",
    "wget -nv -nc https://huggingface.co/jartine/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile\n",
    "\n",
    "# Step 2: Make the llamafile executable. Note: if you're on Windows, just append '.exe' to the filename.\n",
    "chmod +x TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile\n",
    "\n",
    "# Step 3: Start llamafile server in background. All the server logs will be written to 'tinyllama.log'.\n",
    "# Alternatively, you can just open a separate terminal outside this notebook and run: \n",
    "#   ./TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile --server --nobrowser --embedding\n",
    "./TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile --server --nobrowser --embedding > tinyllama.log 2>&1 &\n",
    "pid=$!\n",
    "echo \"${pid}\" > .llamafile_pid  # write the process pid to a file so we can terminate the server later"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3188b22f-879f-47b3-9a27-24412f6fad5f",
   "metadata": {},
   "source": [
    "## Embedding texts using LlamafileEmbeddings\n",
    "\n",
    "Now, we can use the `LlamafileEmbeddings` class to interact with the llamafile server that's currently serving our TinyLlama model at http://localhost:8080."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0be1af71",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.embeddings import LlamafileEmbeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c66e5da",
   "metadata": {},
   "outputs": [],
   "source": [
    "embedder = LlamafileEmbeddings()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01370375",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"This is a test document.\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a42e4035",
   "metadata": {},
   "source": [
    "To generate embeddings, you can either query an invidivual text, or you can query a list of texts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91bc875d-829b-4c3d-8e6f-fc2dda30a3bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "query_result = embedder.embed_query(text)\n",
    "query_result[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4b0d49e-0c73-44b6-aed5-5b426564e085",
   "metadata": {},
   "outputs": [],
   "source": [
    "doc_result = embedder.embed_documents([text])\n",
    "doc_result[0][:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ccc78fc-03ae-411d-ae73-74a4ee91c725",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "# cleanup: kill the llamafile server process\n",
    "kill $(cat .llamafile_pid)\n",
    "rm .llamafile_pid"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  },
  "vscode": {
   "interpreter": {
    "hash": "e971737741ff4ec9aff7dc6155a1060a59a8a6d52c757dbbe66bf8ee389494b1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
